package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.serverpackets.UserInfo;

/**
 * 客户端请求分配点数
 * 
 * @version $Revision: 1.3.4.2 $ $Date: 2005/03/27 15:29:30 $
 */
public final class RequestAssignPoint extends L2GameClientPacket {

	private static final Logger _log = LoggerFactory.getLogger(RequestAssignPoint.class.getName());

	private static final String _C__25_REQUESTASSIGNPOINT = "[C] 25 RequestAssignPoint";

	private static final byte STR = 0;

	private static final byte DEX = 1;

	private static final byte INT = 2;

	private static final byte MEN = 3;

	private static final byte CON = 4;

	class Assign {

		byte _attr;

		int _point;

		Assign(byte attr, int point) {
			_attr = attr;
			_point = point;
		}
	}

	private Assign[] _assignPoint;

	@Override
	protected void readImpl() {
		int size = readByte();
		if (size == 0)
			return;
		_assignPoint = new Assign[size];
		for (int i = -1; ++i < size;) {
			_assignPoint[i] = new Assign(readByte(), readInt());
		}
	}

	@Override
	protected void runImpl() {
		FuzePcInstance _activeChar = getClient().getActiveChar();
		if (_activeChar == null || _assignPoint == null)
			return;
		boolean deal = false;
		for (int i = -1; ++i < _assignPoint.length;) {
			//如果未分配的点数已经不够分配，则退出
			if (_activeChar.getUnassignedPoint() < _assignPoint[i]._point || _assignPoint[i]._point <= 0)
				break;
			deal = true;
			//减掉未分配的点数
			int old = _activeChar.getStat().getUnassignedPoint();
			_activeChar.getStat().setUnassignedPoint(_activeChar.getUnassignedPoint() - _assignPoint[i]._point);
			_log.info("分配点数:分配前:" + old + ",分配后:" + _activeChar.getStat().getUnassignedPoint(), _activeChar.getObjectId(), _activeChar.getName());
			switch (_assignPoint[i]._attr) {
			case STR:
				old = _activeChar.getAssignedSTR();
				_activeChar.setAssignedSTR(_activeChar.getAssignedSTR() + _assignPoint[i]._point);
				_log.info("分配STR:分配前:" + old + ",分配后:" + _activeChar.getAssignedSTR(), _activeChar.getObjectId(), _activeChar.getName());
				break;
			case DEX:
				old = _activeChar.getAssignedDEX();
				_activeChar.setAssignedDEX(_activeChar.getAssignedDEX() + _assignPoint[i]._point);
				_log.info("分配DEX:分配前:" + old + ",分配后:" + _activeChar.getAssignedDEX(), _activeChar.getObjectId(), _activeChar.getName());
				break;
			case INT:
				old = _activeChar.getAssignedINT();
				_activeChar.setAssignedINT(_activeChar.getAssignedINT() + _assignPoint[i]._point);
				_log.info("分配INT:分配前:" + old + ",分配后:" + _activeChar.getAssignedINT(), _activeChar.getObjectId(), _activeChar.getName());
				break;
			case MEN:
				old = _activeChar.getAssignedMEN();
				_activeChar.setAssignedMEN(_activeChar.getAssignedMEN() + _assignPoint[i]._point);
				_log.info("分配MEN:分配前:" + old + ",分配后:" + _activeChar.getAssignedMEN(), _activeChar.getObjectId(), _activeChar.getName());
				break;
			case CON:
				old = _activeChar.getAssignedCON();
				_activeChar.setAssignedCON(_activeChar.getAssignedCON() + _assignPoint[i]._point);
				_log.info("分配CON:分配前:" + old + ",分配后:" + _activeChar.getAssignedCON(), _activeChar.getObjectId(), _activeChar.getName());
				break;
			}
		}
		if (deal) {
			_activeChar.sendPacket(new UserInfo(_activeChar));
		}
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__25_REQUESTASSIGNPOINT;
	}
}
